package com.backend.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.util.CommonPage;
import com.util.CommonResult;
import com.common.model.User;
import com.common.query.UserQueryParam;
import com.common.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "UserController", value = "用户信息管理")
public class UserController {

    @Resource
    UserService userService;

    @RequestMapping("/")
    public String index() {
        return "Hello!";
    }

    @ApiOperation(value = "获取用户列表")
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<User>> index(
            UserQueryParam userQueryParam,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "5") Integer pageSize
    ) {
        IPage<User> userList = userService.findAll(page, pageSize, userQueryParam);
        return CommonResult.success(CommonPage.restPage(userList));
    }

    @ApiOperation(value = "创建用户")
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(
            @RequestBody @Validated User user,
            BindingResult bindingResult
    ) {

        if (bindingResult.hasErrors()) {
            String error = bindingResult.getAllErrors().get(0).getDefaultMessage();
            return CommonResult.failed(error);
        }

        int result = userService.save(user);
        if (result > 0) {
            return CommonResult.success(result);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "获取单个用户信息")
    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    public CommonResult<User> getInfo(
            @PathVariable("id") Long id
    ) {
        User user = userService.findByUserId(id);
        return CommonResult.success(user);
    }

    @ApiOperation(value = "更新用户")
    @RequestMapping(value = "/user/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public CommonResult update(
            @PathVariable("id") Long id,
            @RequestBody @Validated User user,
            BindingResult bindingResult
    ) {
        
        if (bindingResult.hasErrors()) {
            String error = bindingResult.getAllErrors().get(0).getDefaultMessage();
            return CommonResult.failed(error);
        }

        userService.edit(id, user);

        return CommonResult.success("更新成功");
    }

    @ApiOperation(value = "删除用户")
    @RequestMapping(value = "/user/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult<String> delete(
            @PathVariable("id") Long id
    ) {
        userService.delete(id);
        return CommonResult.success("操作成功");
    }

    @ApiOperation(value = "搜索用户")
    @RequestMapping(value = "/search/user", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Map<Long, String>> search(
            @RequestParam(value = "username", required = false) String username
    ) {
        List<User> userList;
        if(username == null){
            userList = userService.findAll();
        }else {
            userList = userService.findByUsernameLike(username);
        }

        Map<Long, String> result = new HashMap<>();
        userList.forEach(user -> result.put(user.getUserId(), user.getUsername()));

        return CommonResult.success(result);
    }


}
